package cn.hu.order.controller;

import cn.hu.order.service.IOrderAdoptService;
import cn.hu.order.domain.OrderAdopt;
import cn.hu.order.query.OrderAdoptQuery;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.AjaxResult;
import cn.hu.basic.util.PageList;
import cn.hu.pet.domain.Pet;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/orderAdopt")
public class OrderAdoptController {
    @Autowired
    public IOrderAdoptService orderAdoptService;


    /**
     * 保存和修改公用的
     * @param orderAdopt 传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrUpdate(@RequestBody OrderAdopt orderAdopt) {
        if ( orderAdopt.getId() != null){
            orderAdoptService.update(orderAdopt);
        }else{
            orderAdoptService.add(orderAdopt);
        }
        return AjaxResult.getInstance();
    }

    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value = "/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        orderAdoptService.delete(id);
        return AjaxResult.getInstance();
    }

    //获取用户
    @GetMapping("/{id}")
    public OrderAdopt get(@PathVariable("id") Long id) {
        return orderAdoptService.findById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<OrderAdopt> list() {
        return orderAdoptService.findAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<OrderAdopt> json(@RequestBody OrderAdoptQuery query) {
        return orderAdoptService.listByQuery(query);
    }

    @PostMapping("/place_order")
    public AjaxResult placeOrder(@RequestBody Pet pet, HttpServletRequest request) throws Exception {
        orderAdoptService.placeOrder(pet,request);
        return AjaxResult.getInstance();
    }
}
